package dao;

import java.sql.*;

import modle.Customer;

public class CustomerDao {

	//登录：从数据库中查询特定的Customer的信息
		public static Customer login(Connection con,Customer user) throws Exception {
			Customer resultUser = null;
			
			//先预编译（编译的是sql语句的框架）后传值（? 是占位符，会自动加单引号）
			String sql = "select * from customer where UserName = ? and PassWord = ?";
			
			//表示（获取）预编译的SQL语句的对象。
			PreparedStatement pstmt = con.prepareStatement(sql);
			
			pstmt.setString(1, user.getUserName());
			pstmt.setString(2, user.getPassWord());
			ResultSet rs = pstmt.executeQuery();			//返回表的查询集合（应该是从上一个指针开始）
			
			if(rs.next()) {
				resultUser = new Customer();
				resultUser.setUserName(rs.getString("UserName"));
				resultUser.setPassWord(rs.getString("PassWord"));
				resultUser.setId(rs.getInt("Id"));
				System.out.println("-----" + rs.getInt("Id"));
			}
			
			return resultUser;
		}
		
		public static int Add(Connection con, Customer user) throws Exception {
			
			String sql = "insert into `customer` values(?,?,?,?,?,null)";
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, user.getUserName());
			pstmt.setString(2, user.getPassWord());
			pstmt.setString(3, user.getName());
			pstmt.setString(4, user.getAddress());
			pstmt.setString(5, user.getTelephone());
			
			return pstmt.executeUpdate();
		}
		
		public static boolean isUserExist(Connection con, Customer user) throws Exception {
			// TODO Auto-generated method stub
			String sql="select * from customer where UserName=?";
			PreparedStatement pstmt=con.prepareStatement(sql);
			pstmt.setString(1, user.getUserName());
			ResultSet rs =pstmt.executeQuery();
			return rs.next();
		}
}
